Params order in Foo.new(params[:foo]), need one before the other (Rails)

Posted by Jeena on Stack Overflow See other posts from Stack Overflow or by Jeena
Published on 2010-03-29T22:35:59Z Indexed on 2010/03/29 23:13 UTC
Read the original article Hit count: 447

Filed under:

I have a problem which I don't know how to fix. It has to do with the unsorted params hash.

I have a object Reservation which has a virtual time= attribute and a virtual eating_session= attribute when I set the time= I also want to validate it via an external server request. I do that with help of the method times() which makes a lookup on a other server and saves all possible times in the @times variable.

The problem now is that the method times() needs the eating_session attribute to find out which times are valid, but rails sometimes calls the times= method first, before there is any eating_session in the Reservation object when I just do @reservation = Reservation.new(params[:reservation])

class ReservationsController < ApplicationController
    def new
        @reservation = Reservation.new(params[:reservation])
        # ...
    end
end

class Reservation < ActiveRecord::Base
    include SoapClient
    attr_accessor :date, :time
    belongs_to :eating_session

    def time=(time)
        @time = times.find { |t| t[:time] == time }
    end

    def times
        return @times if defined? @times
        @times = []
        response = call_soap :search_availability {
            # eating_session is sometimes nil
            :session_id => eating_session.code, # <- HERE IS THE PROBLEM
            :dining_date => date
        }
        response[:result].each do |result|
            @times << {
                :time => "#{DateTime.parse(result[:time]).strftime("%H:%M")}",
                :correlation_data => result[:correlation_data]
            }
        end
        @times
    end
end

I have no idea how to fix this, any help is apriciated.

© Stack Overflow or respective owner

Related posts about ruby-on-rails